package com.shengxing.lljl.controller;


import com.shengxing.lljl.common.Result;
import com.shengxing.lljl.dto.UserDto;
import com.shengxing.lljl.dto.UserLoginDto;
import com.shengxing.lljl.entity.User;
import com.shengxing.lljl.service.IUserService;
import com.shengxing.lljl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表-存储用户信息 前端控制器
 * </p>
 *
 * @author 小邹
 * @since 2024-10-17
 */
@Api(value = "User Controller", tags = "用户")
@RestController
@Slf4j
@RequestMapping("/lljl/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "主页面返回用户数据")
    @GetMapping("/main/getUser")
    private Result<UserVo> getUser(@RequestParam String email) {
        log.info("查询email:{}", email);
        UserVo userVo = userService.getByEmail(email);
        if (userVo == null) {
            return Result.error("用户不存在");
        }
        return Result.success(userVo);
    }

    //    @CrossOrigin
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    private Result<String> login(@RequestBody UserLoginDto userLoginDto) {

        log.info("员工登录：{}", userLoginDto);
        User user = new User();
        user.setUserPlateform(userLoginDto.getUserPlateform());
        user.setEmail(userLoginDto.getEmail());
        user.setPassword(userLoginDto.getPassword());


        if (userService.loginService(user) == null) {
            return Result.error("登录失败");
        } else {
            return Result.success("登录成功");
        }
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    private Result<String> register(@RequestBody UserDto userDto) {
//        User user = new User();
//        user.setUserPlatform(userDto.getUserPlateform());
//        user.setEmail(userDto.getEmail());
//        user.setPassword(userDto.getPassword());
//        user.setAccountFunds(new BigDecimal(0));
//
//        if (userService.registerService(user) == null) {
//            return Result.error("注册失败");
//        } else {
//            return Result.success("注册成功");
//        }
        log.info("员工注册：{}", userDto);

        String r = userService.save(userDto);
        if (r.equals("SUCCESS")) {
            return Result.success(r);
        }
        return Result.error(r);
    }

    @ApiOperation(value = "邮箱校验")
    @GetMapping("/register/sendEmailCode")
    public Result<String> sendEmailCode(@RequestParam String email) {
        log.info("发送邮箱验证码 : {}", email);
        String s = userService.sendEmailCode(email);

        return Result.success(s);
    }

    @ApiOperation(value = "更改邮箱")
    @PostMapping("/{userId}/updateEmail")
    public Result<String> updateNickname(@PathVariable Long userId, @RequestParam String newEmail) {
        log.info("更改邮箱{}", userId, newEmail);
        User user = new User();
        user.setId(userId);
        user.setEmail(newEmail);

        Boolean success = userService.updateEmailService(user);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @ApiOperation(value = "更改密码")
    @PostMapping("/{userId}/updatePassword")
    public Result<String> updatePassword(@PathVariable Long userId, @RequestParam String newPassword) {
        log.info("更改密码：{}", userId, newPassword);
        User user = new User();
        user.setId(userId);
        user.setPassword(newPassword);

        Boolean success = userService.updatePasswordService(user);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @ApiOperation(value = "更新用户")
    @PostMapping("/{userId}/account")
    public Result<String> updateAccount(@RequestBody User user) {
        log.info("更新用户：{}", user);
        if (user == null) {
            return Result.error("用户不存在");
        }

        Boolean success = userService.updatePasswordService(user);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }


    @PostMapping("/find")
    private User find(long user_id) {
        log.info("测试1");
        return userService.getById(user_id);
    }


    @ApiOperation(value = "测试1")

    @GetMapping("/test")
    private String test1() {
        log.info("测试路径");
        return "hello world";
    }

//    @ApiOperation(value = "测试2")
//    @RequestMapping("/name")
//    private String test2(@RequestParam(value = "name")String name){
//        return name;
//    }
}
